{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用Appbuilder-SDK Trace框架实现对Client的跟踪\n",
    "\n",
    "- 如果没有安装phoenix，需要先安装phoenix，这里建议使用清华源镜像安装，推荐使用4.5.0版本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "python3 -m pip install arize-phoenix==4.5.0 -i https://pypi.tuna.tsinghua.edu.cn/simple"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 启动phoenix服务"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "appbuilder_trace_server"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- ctrl+鼠标左键点击http://localhost:8080/进入可视化界面,跟踪的信息将在这里可视化展示\n",
    "![phoenix可视化界面](https://bj.bcebos.com/v1/appbuilder-sdk-components/Phoenix%E5%8F%AF%E8%A7%86%E5%8C%96%E7%95%8C%E9%9D%A2%EF%BC%881%EF%BC%89.png?authorization=bce-auth-v1%2FALTAKGa8m4qCUasgoljdEDAzLm%2F2024-07-22T04%3A41%3A00Z%2F-1%2Fhost%2F19a246ca50757aa64a3ccabfac0c8cd93730a08599201dd81e9de9bb2f9bb2a3)\n",
    "- 使用ctrl+c停止phoenix服务\n",
    "\n",
    "### 启动Appbuilder-SDK TRACE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024-07-03 11:10:00,040.040] tracer.py [line:128] INFO [main-9857193014994891849] OTLPSpanExporter endpoint: http://localhost:8080/v1/traces\n"
     ]
    }
   ],
   "source": [
    "from appbuilder import AppBuilderTracer\n",
    "tracer=AppBuilderTracer(\n",
    "    enable_phoenix = True,\n",
    "    enable_console = False,\n",
    "    )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- enable_phoenix:是否启动phoenix可视化跟踪\n",
    "- enable_console:是否将trace信息反馈到控制台"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024-07-03 11:10:03,800.800] tracer.py [line:162] INFO [main-13600015437371441059] AppBuilder Starting trace...\n",
      "answer='' events=[Event(code=0, message='', status='done', event_type='function_call', content_type='function_call', detail={'text': {'arguments': {}, 'component_code': 'ChatAgent', 'component_name': '聊天助手'}}, usage=None)]\n",
      "answer='' events=[Event(code=0, message='', status='preparing', event_type='ChatAgent', content_type='status', detail={}, usage=None)]\n",
      "answer='作为植物' events=[Event(code=0, message='', status='running', event_type='ChatAgent', content_type='text', detail={'text': '作为植物'}, usage=Usage(prompt_tokens=77, completion_tokens=0, total_tokens=77, name='ERNIE-4.0-8K'))]\n",
      "answer='识别专家，我可以帮助您识别各种植物种类。' events=[Event(code=0, message='', status='running', event_type='ChatAgent', content_type='text', detail={'text': '识别专家，我可以帮助您识别各种植物种类。'}, usage=Usage(prompt_tokens=77, completion_tokens=0, total_tokens=77, name='ERNIE-4.0-8K'))]\n",
      "answer='如果您提供植物的图片或详细描述，我可以利用植物识别组件来分析并确定植物的种类，为您提供准确、专业的识别结果。' events=[Event(code=0, message='', status='running', event_type='ChatAgent', content_type='text', detail={'text': '如果您提供植物的图片或详细描述，我可以利用植物识别组件来分析并确定植物的种类，为您提供准确、专业的识别结果。'}, usage=Usage(prompt_tokens=77, completion_tokens=0, total_tokens=77, name='ERNIE-4.0-8K'))]\n",
      "answer='无论是常见的花卉、树木，还是稀有的植物品种，我都可以尽力为您提供识别服务。' events=[Event(code=0, message='', status='running', event_type='ChatAgent', content_type='text', detail={'text': '无论是常见的花卉、树木，还是稀有的植物品种，我都可以尽力为您提供识别服务。'}, usage=Usage(prompt_tokens=77, completion_tokens=0, total_tokens=77, name='ERNIE-4.0-8K'))]\n",
      "answer='' events=[Event(code=0, message='', status='done', event_type='ChatAgent', content_type='text', detail={'text': ''}, usage=Usage(prompt_tokens=77, completion_tokens=60, total_tokens=137, name='ERNIE-4.0-8K'))]\n",
      "answer='' events=[Event(code=0, message='', status='success', event_type='ChatAgent', content_type='status', detail={}, usage=None)]\n",
      "answer='' events=[]\n",
      "[2024-07-03 11:10:11,965.965] tracer.py [line:166] INFO [main-9471189008456186581] AppBuilder Ending trace...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:appbuilder:AppBuilder Ending trace...\n"
     ]
    }
   ],
   "source": [
    "# 启动trace \n",
    "import os\n",
    "import appbuilder\n",
    "\n",
    "tracer.start_trace()\n",
    "\n",
    "# 这里将APPBUILDER_TOKEN与app_id更换为你的APPBUILDER_TOKEN和app_id\n",
    "os.environ[\"APPBUILDER_TOKEN\"] = \"YOUR_APPBUILDER_TOKEN\"\n",
    "app_id = \"YOUR_APP_ID\"\n",
    "\n",
    "builder = appbuilder.AppBuilderClient(app_id)\n",
    "conversation_id = builder.create_conversation()\n",
    "msg = builder.run(conversation_id=conversation_id, query=\"你可以做什么？\",stream=True)\n",
    "\n",
    "for m in msg.content:\n",
    "    print(m)\n",
    "\n",
    "# 结束trace\n",
    "tracer.end_trace()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### phoneix可视化跟踪界面展示\n",
    "![phoenix可视化页面展示结果](https://bj.bcebos.com/v1/appbuilder-sdk-components/Phoenix%E5%8F%AF%E8%A7%86%E5%8C%96%E7%95%8C%E9%9D%A2%EF%BC%882%EF%BC%89.png?authorization=bce-auth-v1%2FALTAKGa8m4qCUasgoljdEDAzLm%2F2024-07-22T04%3A41%3A31Z%2F-1%2Fhost%2F86460e40ecab90c50168da46ca0cf79f6179696943e1a1546f9a9ce6d34b8063)\n",
    "- 可以在可视化界面查看跟踪信息如Client组件的调用链路，输入输出、消耗token数等信息\n",
    "- 点击某个调用链路，可以查看该调用链路详细信息，流式运行过程，HTTP-POST节点展示curl命令\n",
    "![phoenix可视化页面节点展示结果](https://bj.bcebos.com/v1/appbuilder-sdk-components/Phoenix%E5%8F%AF%E8%A7%86%E5%8C%96%E7%95%8C%E9%9D%A2%EF%BC%883%EF%BC%89.png?authorization=bce-auth-v1%2FALTAKGa8m4qCUasgoljdEDAzLm%2F2024-07-22T04%3A41%3A46Z%2F-1%2Fhost%2F096835857c7de8714250a260a0373e69938c91d5452e3955d3f4b2ca298b8bcc)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
